package restaurant;

import java.util.ArrayList;

/**
 * Represents the restaurant customers' tabs. The {@code CustomerTabs} class is
 * a collection of {@code Tab} objects. In order to access a {@code Tab}, the
 * program must go through the {@code CustomerTabs} class.
 * 
 * @author Ebonie
 * @author Odessa
 * @see Tab
 */

public class CustomerTabs
{

	/** The list of tabs. */
	private ArrayList<Tab> customerTabs;

	/** The tab number. */
	private int tabNumber = 1;



	/**
	 * Instantiates the customer tabs.
	 */
	public CustomerTabs() {
		customerTabs = new ArrayList<Tab>();
	}



	/**
	 * Gets the tabs.
	 * 
	 * @return the list of tabs.
	 */
	public ArrayList<Tab> getTabs()
	{
		return customerTabs;
	}



	/**
	 * Creates a new tab. The {@code tabNumber} is used so that each tab created
	 * has a unique tab number. After the tab is created, it is added to the
	 * customerTabs and the {@code tabNumber} gets incremented.
	 * 
	 * @param tableNo the table number for this tab.
	 * @return the newly created tab.
	 */
	public Tab createTab(int tableNo)
	{

		Tab t = new Tab(tabNumber, tableNo);
		tabNumber++;
		addTab(t);

		return t;
	}



	/**
	 * Adds the {@code tab} to the customerTabs ArrayList. Takes a tab and
	 * appends it to the end of the list.
	 * 
	 * @param tab the tab to be added to the customerTabs.
	 */
	private void addTab(Tab tab)
	{
		customerTabs.add(tab);
	}



	/**
	 * Adds items to the tab.
	 * 
	 * @param tabNo the tab number.
	 * @param itemNo the item to be added to the tab.
	 * @param price the price of the item to be added to the tab.
	 */
	public void addToTab(int tabNo, int itemNo, double price)
	{
		Tab t = find(tabNo);
		t.addItems(itemNo, price);
	}



	/**
	 * Gets the tab for the specified table. It first finds the tab in the
	 * customerTabs and then returns it.
	 * 
	 * @param tableNo the table number of the table to return.
	 * @return the tab for the table.
	 */
	public Tab getTableTab(int tableNo)
	{
		return findOpenTab(tableNo);
	}



	/**
	 * Splits a tab. It first finds the tab in the customerTabs and then splits
	 * the tab based on {@code splitInto}.
	 * 
	 * @param tabNo the tab number of the tab to split.
	 * @param splitInto the number to split this tab into.
	 */
	public void splitTab(int tabNo, int splitInto)
	{
		Tab tab = find(tabNo);
		tab.split(splitInto);
	}



	/**
	 * Closes a tab. It first finds the tab in the customerTabs and then closes
	 * the tab.
	 * 
	 * @param tabNo the tab number of the tab to close.
	 */
	public void closeTab(int tabNo)
	{
		Tab tab = find(tabNo);
		tab.close();
	}



	/**
	 * Finds a tab by its tab number. It looks at all the tabs in the
	 * customerTabs and returns the tab with the specified tab number.
	 * 
	 * @param tabNo the tab number of the tab to find.
	 * @return the tab matching the tab number entered.
	 */
	private Tab find(int tabNo)
	{
		Tab tab = null;

		for (Tab t : customerTabs) {
			int id = t.getTabNo();

			if (id == tabNo) {
				tab = t;
				break;
			}
		}

		return tab;
	}



	/**
	 * Finds an open tab by the {@code tableNo} specified. It looks at all the
	 * tabs in the customerTabs and returns the tab with the specified table
	 * number if it is open.
	 * 
	 * @param tableNo the table number of the tab to find.
	 * @return the tab matching the tab number entered.
	 */
	private Tab findOpenTab(int tableNo)
	{
		Tab tab = null;

		for (Tab t : customerTabs) {
			int table_id = t.getTableNo();

			if (table_id == tableNo) {
				tab = t;

				if (tab.isOpen())
					break;
			}
		}

		return tab;
	}

}
